<?php
            
use app\model\Theme; 


/**
 * Author: cbw
 * Desc: 抖音商城项目
 * Time: 2021/11/15 3:02 下午
 * Email: 17854288794@163.com
 */
class Gift_Page extends PluginWebPage {
    function main()
    {
        global $_G,$_P,$_S,$_C;
        $action = 6;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        $name = isset($_P['name'])?trim($_P['name']):'';
        $page_index = max(1,intval($_P['page']));
        $page_size = 10;
        $page_start = ($page_index-1)*$page_size;
        $shangjiaid = isset($_P['shangjiaid'])?$_P['shangjiaid']:0;
        $shangjia = BeilaiModel::getShangjiaList();
        array_unshift($shangjia,['id'=>0,'name'=>'全部商家']);
        $lists = BeilaiModel::getGiftList($themeid,$shangjiaid,$name,0,0,'',-1,true);
        $list = BeilaiModel::getGiftList($themeid,$shangjiaid,$name,$page_start,$page_size);
        foreach ($list as $key =>$val){
            $has_flag = db_get(t_shangjia_user_gift,['themeid'=>$themeid,'gift_id'=>$val['id']]);
            if ($has_flag){
                $list[$key]['can_del'] = false;
            }else{
                $list[$key]['can_del'] = true;
            }
        }
        $total = $page_total = count($lists);
        $pager = pagination($page_total,$page_index,$page_size);
        $return_clicent = get_defined_vars();
return $this->view('/beilai/gift/main', $return_clicent);
    }

    function add()
    {
        return $this->post();
    }
    function edit()
    {
        return $this->post();
    }

//    function del()
//    {
//        global $_G, $_P;
//        $id = intval($_P['id']);
//        $themeid = intval($_P['themeid']);
//        $res = db_delete(t_shangjia_gift,['id'=>$id,'themeid'=>$themeid]);
//        if (!$res){
//            show_json(0,['msg'=>'操作失败']);
//        }else{
//            show_json(1,['msg'=>'操作成功']);
//        }
//    }

    protected function post()
    {
        global $_G, $_P;
        $action = 6;
        $id = intval($_P['id']);
        $themeid = intval($_P['themeid']);
        $shangjiaid = intval($_P['shangjiaid']);
        $shangjia = BeilaiModel::getShangjiaList();
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        if (!$themeid){
            show_json(0,'活动入口不正确');
            return;
        }

        if ($_G['ispost'] && $themeid ) {
            if (!$shangjiaid){
                show_json(0,'礼品商家不正确');
            }
            $name = trim($_P['name']);
            $top_img = tomedia($_P['top_img']);
            $price = trim($_P['price']);
            $qty = isset($_P['qty'])?$_P['qty']:1000;
            $status = $_P['status'];
            $type = $_P['type'];
            $detail = $_P['detail'];
            if (!$name){
                show_json(0,'礼品名称不能为空');
            }
            if (!$top_img){
                show_json(0,'礼品头图未设置');
            }
            if (!$qty){
                show_json(0,'礼品当前库存未设置');
            }
            if (!$price){
                show_json(0,'礼品市场价格未设置');
            }

            $data = [
                'shangjiaid'=>$shangjiaid,
                'name'=>$name,
                'top_img'=>$top_img,
                'qty'=>$qty,
                'price'=>$price,
                'status'=>$status,
                'type'=>$type,
                'themeid'=>$themeid,
                'detail'=>$detail,
            ];
            if (!$id){

                $res = BeilaiModel::insertGift($data);
            }else{
                $where = ['id'=>$id];
                $res = BeilaiModel::editGiftItem($where,$data);
            }
            if (!$res){
                show_json(0,['msg'=>'操作失败']);
            }else{
                show_json(1,['msg'=>'操作成功']);
            }
            die();
        }
        if ($id && $themeid ){
            $item = BeilaiModel::getGiftItemById($themeid,$id);
        }
        $return_clicent = get_defined_vars();
return $this->view('/beilai/gift/edit', $return_clicent);
    }

    function editItemQty()
    {
        global $_G, $_P;
        $action = 5;
        $id = intval($_P['id']);
        $themeid = intval($_P['themeid']);
        $op_num = intval($_P['op_num']);
        $action = trim($_P['action']);
        $new_qty = 0;

        $item = BeilaiModel::getCourseItemById($themeid,$id);
        if ($action == 'add'){
            $new_qty = $item['qty']+$op_num;
        }else{
            $new_qty = max(0,($item['qty']-$op_num));
        }
        $where = ['id'=>$id];
        $data = ['qty'=>$new_qty];
        $res = BeilaiModel::editCourseItem($where,$data);
        if (!$res){
            show_json(0,['msg'=>'操作失败']);
        }else{
            show_json(1,['msg'=>'操作成功']);
        }
    }
}